package com.dyh.quickdemozwei.withdraw.biz;

import com.alibaba.fastjson.JSONObject;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;

/**
 * @Author: yhdong
 * @Date: 2021/11/30 13:58
 */
@Slf4j
@Service
public class ProductBiz {

    public boolean put(PutContext putContext){
        log.info("新建商品 {}",putContext.productId);
        return true;
    }


    public boolean putFalse(PutContext putContext){
        log.info("新建商品 {}",putContext.productId);
        return false;
    }


    public boolean put(String id,JSONObject body){
        log.info("新建商品 {}",id);
        return true;
    }

    public boolean putAttr(String id,JSONObject body){
        log.info("新建商品属性 {}",id);
        return true;
    }

    public boolean  deleteAttr(String id){
        log.info("删除商品属性 {}",id);
        return true;
    }


    public boolean putAttr(PutAttrContext putAttrContext){
        log.info("新建商品属性 {}",putAttrContext.attrId);
        return true;
    }

    public boolean delete(String id){
        log.info("删除商品 {}",id);
        return true;
    }

    @Setter@Getter@AllArgsConstructor
    public static class PutContext{
        String productId;
        JSONObject productBoby;
    }

    @Setter@Getter@AllArgsConstructor
    public static class PutAttrContext{
        String attrId;
        JSONObject attrBody;
    }


    @Setter@Getter
   public  static  class FullNagareContext{

       PutContext masterProd;

       PutContext subProd;

       PutAttrContext putAttrContext;


   }


    public void fullNagare(FullNagareContext fullNagareContext){
        put(fullNagareContext.masterProd.productId,fullNagareContext.masterProd.productBoby);
        putAttr(fullNagareContext.putAttrContext.attrId,fullNagareContext.putAttrContext.attrBody);
        put(fullNagareContext.subProd.productId,fullNagareContext.subProd.productBoby);
    }

    public void fullNagareKai(FullNagareContext fullNagareContext){

        List<Function<FullNagareContext,Boolean>> actions = new ArrayList<>();

        actions.add(e-> put(e.masterProd.productId,e.masterProd.productBoby));
        actions.add(e-> putAttr(e.putAttrContext.attrId,e.putAttrContext.attrBody));
//        actions.add(e-> {throw new RuntimeException("新增attr报错");});

//        actions.add(e-> put(e.subProd.productId,e.subProd.productBoby));
//        actions.add(e->{throw new RuntimeException("新增子商品报错");} );
        actions.add(e-> putFalse(e.getSubProd()));


        List<Function<FullNagareContext,Boolean>> deActions = new ArrayList<>();
        deActions.add(e->delete(e.masterProd.productId));
        deActions.add(e->deleteAttr(e.putAttrContext.attrId));


        int c = 0;
        for (int i = 0;i<actions.size();i++,c++){
            Function<FullNagareContext,Boolean> action = actions.get(i);
            try {
               boolean res =  action.apply(fullNagareContext);
               if (!res)//结果失败
                   break;
            } catch (Exception e) {
                break;
            }
        }

        if (c==actions.size())
            return;

        c--;
        for (;c>=0;c--){
            Function<FullNagareContext,Boolean> deAction = deActions.get(c);
            deAction.apply(fullNagareContext);
        }




    }



}
